package com.csair.irp.job.file.task;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class DispatchLogService extends ServiceImpl<DispatchLogMapper, DispatchLogPO> {

    /**
     * 获取需要执行的调度任务
     *
     * @param taskType
     * @param beginDate
     * @param endDate
     * @param states
     * @return
     */
    public List<DispatchLogPO> listNeedExecute(TaskType taskType, LocalDate beginDate, LocalDate endDate, DispatchLogPO.StateEnum[] states) {
        int[] statesCode = new int[states.length];
        int index = 0;
        for (DispatchLogPO.StateEnum state : states) {
            statesCode[index] = state.getCode();
        }
        return baseMapper.listNeedExecute(taskType.getDataFileClass(), taskType.getFilePrefix(), beginDate, endDate, statesCode);
    }

    /**
     * 更新调度任务状态和调度任务执行时长
     *
     * @param stateEnum    调度任务解析状态
     * @param dispatchId   调度任务ID
     * @param dispatchTime 调度任务解析时长，单位毫秒
     * @return
     */
    public boolean updateEtlStateById(DispatchLogPO.StateEnum stateEnum, long dispatchId, long dispatchTime) {
        DispatchLogPO dispatchLog = new DispatchLogPO();
        dispatchLog.setEtlState(stateEnum.getCode());
        dispatchLog.setId(dispatchId);
        dispatchLog.setDispatchTime(dispatchTime);
        return updateById(dispatchLog);
    }

    /**
     * 更新调度任务状态
     *
     * @param stateEnum  调度任务解析状态
     * @param dispatchId 调度任务ID
     * @return
     */
    public boolean updateEtlStateById(DispatchLogPO.StateEnum stateEnum, long dispatchId) {
        DispatchLogPO dispatchLog = new DispatchLogPO();
        dispatchLog.setEtlState(stateEnum.getCode());
        dispatchLog.setId(dispatchId);
        return updateById(dispatchLog);
    }


}
